import scrapy
from flask import current_app
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from app import evn_
from spiders.items import StockFundsItem
from utils.util import CalculationTool


class StockFunds2Spider(scrapy.Spider):
    name = 'stock_funds_2'
    stock_list = []

    def start_requests(self):
        '''
        代替start_urls,从数据库读取板块编码，拼接读取股票信息
        :return: 示例：http://data.eastmoney.com/bkzj/BK0537.html
        http://quote.eastmoney.com/sz002240.html
        http://quote.eastmoney.com/concept/sh600031.html#
        '''
        url_1 = 'http://quote.eastmoney.com/%s.html'

        for stockInfo in self.stock_list:
            stock_code = getattr(stockInfo, 'stock_code')
            market = getattr(stockInfo, 'market')
            if stock_code and market:
                code = market + stock_code
                meta = {'name': getattr(stockInfo, "stock_name"), 'code': stock_code}
                yield Request(url_1 % code, callback=self.parse_item, meta=meta)

    def __init__(self, stock_list):
        self.stock_list = stock_list
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)

    def parse_item(self, response):
        '''
        首页涨幅信息
        :param response:
        :return:
        '''
        current_app.logger.info('{}'.format(response))
        stock_code = response.meta["code"]  ##取出数据
        stock_name = response.meta["name"]  ##取出数据

        item = StockFundsItem()
        item['stock_name'] = stock_name
        item['stock_code'] = stock_code

        if stock_code.startswith('68'):
            #  创业板
            # self.gem(response, item)
            current_app.logger.info('{}创业板,不统计'.format(stock_code))
        else:
            self.normal(response, item)
        yield item

    def normal(self, response, item):
        '''非创业板'''

        # 交易状态
        tradestatus = response.xpath('//*[@id="tradestatus"]/text()').extract_first()
        if (tradestatus == "已收盘"):
            item['open'] = response.xpath('//*[@id="rgt11"]/text()').extract_first()
            item['close'] = response.xpath('//*[@id="rgt1"]/text()').extract_first()
            item['high'] = response.xpath('//*[@id="rgt9"]/text()').extract_first()
            item['low'] = response.xpath('//*[@id="rgt10"]/text()').extract_first()
            item['preclose'] = response.xpath('//*[@id="rgt12"]/text()').extract_first()
            item['average'] = response.xpath('//*[@id="rgt2"]/text()').extract_first()  # 均价
            item['pctChg'] = CalculationTool.cutOutTail(
                response.xpath('//*[@id="rgt3"]/text()').extract_first())  # 涨幅  %
            item['volume'] = CalculationTool.cutOutTail(
                response.xpath('//*[@id="rgt5"]/text()').extract_first())  # 成交量  手
            item['amount'] = CalculationTool.convert2Y(
                response.xpath('//*[@id="rgt6"]/text()').extract_first())  # 成交额  万
            item['turn'] = CalculationTool.cutOutTail(response.xpath('//*[@id="gt4"]/text()').extract_first())  # 换手率  %
            item['volume_ratio'] = response.xpath('//*[@id="gt11"]/text()').extract_first()  # 量比

            item['inner'] = CalculationTool.cutOutTail(response.xpath('//*[@id="rgt16"]/text()').extract_first())
            item['outer'] = CalculationTool.cutOutTail(response.xpath('//*[@id="rgt15"]/text()').extract_first())
            # item['amplitude'] = self.cutOutTail(response.xpath('tr[7]/td[1]/span/text()').extract_first()) # 振幅
            item['PE'] = response.xpath('//*[@id="gt6_2"]/text()').extract_first()
            item['PB'] = response.xpath('//*[@id="gt13_2"]/text()').extract_first()  # 市净率
            # # 资金流入情况
            item['main_inflow'] = CalculationTool.convert2W(
                response.xpath('//*[@id="hz_a"]/text()').extract_first())  # 主力流入  万元
            item['main_outflow'] = CalculationTool.convert2W(
                response.xpath('//*[@id="hz_b"]/text()').extract_first())  # 主流流出  万元
            item['main_net_inflow'] = CalculationTool.convert2W(
                response.xpath('//*[@id="hz_c"]/text()').extract_first())  # '主力资金净流入',  万元

            date_span = response.xpath('//*[@id="day"]/text()').extract_first()
            if date_span:
                split = date_span.split(' ')
                item['create_time'] = split[0][1:]
                # item['week_day'] = self.common_week_day[split[1][-1]]
